```html
<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Java TreeMap与TreeSet排序机制解析</title>
    <link href="https://cdn.staticfile.org/font-awesome/6.4.0/css/all.min.css" rel="stylesheet">
    <link href="https://cdn.staticfile.org/tailwindcss/2.2.19/tailwind.min.css" rel="stylesheet">
    <link href="https://fonts.googleapis.com/css2?family=Noto+Serif+SC:wght@400;500;600;700&family=Noto+Sans+SC:wght@300;400;500;700&display=swap" rel="stylesheet">
    <script src="https://cdn.jsdelivr.net/npm/mermaid@latest/dist/mermaid.min.js"></script>
    <style>
        body {
            font-family: 'Noto Sans SC', Tahoma, Arial, Roboto, "Droid Sans", "Helvetica Neue", "Droid Sans Fallback", "Heiti SC", "Hiragino Sans GB", Simsun, sans-serif;
            background-color: #f8f9fa;
            color: #333;
            line-height: 1.6;
        }
        .hero-gradient {
            background: linear-gradient(135deg, #6e8efb 0%, #a777e3 100%);
        }
        .code-block {
            background-color: #2d3748;
            border-radius: 8px;
            position: relative;
        }
        .code-header {
            background-color: #1a202c;
            color: #cbd5e0;
            padding: 0.5rem 1rem;
            border-top-left-radius: 8px;
            border-top-right-radius: 8px;
            font-family: monospace;
            font-size: 0.875rem;
            display: flex;
            justify-content: space-between;
        }
        pre {
            margin: 0;
            padding: 1.25rem;
            overflow-x: auto;
        }
        code {
            color: #e2e8f0;
            font-family: 'SFMono-Regular', Consolas, 'Liberation Mono', Menlo, monospace;
            font-size: 0.875rem;
        }
        .feature-card {
            transition: transform 0.3s ease, box-shadow 0.3s ease;
        }
        .feature-card:hover {
            transform: translateY(-5px);
            box-shadow: 0 20px 25px -5px rgba(0, 0, 0, 0.1), 0 10px 10px -5px rgba(0, 0, 0, 0.04);
        }
        .comparison-table {
            border-collapse: separate;
            border-spacing: 0;
            width: 100%;
            overflow: hidden;
        }
        .comparison-table th, .comparison-table td {
            padding: 1rem;
            text-align: left;
        }
        .comparison-table thead th {
            background-color: #4a5568;
            color: white;
            font-weight: 600;
        }
        .comparison-table tbody tr:nth-child(odd) {
            background-color: #edf2f7;
        }
        .comparison-table tbody tr:nth-child(even) {
            background-color: white;
        }
        .comparison-table tbody tr:hover {
            background-color: #ebf8ff;
        }
        .highlight {
            position: relative;
        }
        .highlight::after {
            content: '';
            position: absolute;
            left: 0;
            bottom: 0.1em;
            width: 100%;
            height: 0.4em;
            background-color: rgba(167, 119, 227, 0.3);
            z-index: -1;
            transform: scaleX(0);
            transform-origin: right;
            transition: transform 0.3s ease;
        }
        .highlight:hover::after {
            transform: scaleX(1);
            transform-origin: left;
        }
    </style>
</head>
<body class="min-h-screen">
    <!-- Hero Section -->
    <section class="hero-gradient text-white py-20 px-4 md:px-0">
        <div class="container mx-auto max-w-5xl">
            <div class="flex flex-col md:flex-row items-center">
                <div class="md:w-1/2 mb-10 md:mb-0">
                    <h1 class="text-4xl md:text-5xl font-bold mb-6 font-serif">Java 有序集合解析</h1>
                    <h2 class="text-2xl md:text-3xl font-semibold mb-6">深入理解 <span class="highlight">TreeMap</span> 与 <span class="highlight">TreeSet</span> 的排序机制</h2>
                    <p class="text-lg md:text-xl opacity-90 mb-8">探索基于红黑树实现的有序集合如何通过 Comparable 和 Comparator 灵活控制元素的排序方式</p>
                    <div class="flex space-x-4">
                        <a href="#intro" class="px-6 py-3 bg-white text-purple-700 rounded-full font-semibold hover:bg-purple-100 transition duration-300">
                            <i class="fas fa-book-open mr-2"></i>开始阅读
                        </a>
                        <a href="#visualization" class="px-6 py-3 bg-transparent border-2 border-white rounded-full font-semibold hover:bg-white hover:text-purple-700 transition duration-300">
                            <i class="fas fa-project-diagram mr-2"></i>可视化图表
                        </a>
                    </div>
                </div>
                <div class="md:w-1/2 flex justify-center">
                    <div class="bg-white bg-opacity-20 p-6 rounded-2xl backdrop-filter backdrop-blur-sm border border-white border-opacity-30 shadow-xl">
                        <div class="code-block">
                            <div class="code-header">
                                <span>Student.java</span>
                                <span><i class="fas fa-code"></i></span>
                            </div>
                            <pre><code>public class Student implements Comparable&lt;Student&gt; {
    private String name;
    private int score;

    @Override
    public int compareTo(Student other) {
        return Integer.compare(this.score, other.score);
    }
    
    // 构造函数、getter、setter...
}</code></pre>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </section>

    <!-- Main Content -->
    <main class="container mx-auto max-w-5xl px-4 py-12">
        <!-- Introduction Section -->
        <section id="intro" class="mb-16">
            <div class="bg-white rounded-xl shadow-md overflow-hidden">
                <div class="p-8">
                    <div class="flex items-center mb-6">
                        <div class="w-12 h-12 rounded-full bg-purple-100 flex items-center justify-center mr-4">
                            <i class="fas fa-info-circle text-purple-600 text-xl"></i>
                        </div>
                        <h2 class="text-2xl font-bold text-gray-800">基本概念</h2>
                    </div>
                    <p class="text-gray-700 mb-6">在 Java 中，<span class="font-semibold text-purple-600">TreeMap</span> 和 <span class="font-semibold text-purple-600">TreeSet</span> 是基于红黑树（Red-Black Tree）实现的有序集合。它们通过排序存储元素，这使得它们特别适合用于需要快速查找、有序输出和范围查询的场景。</p>
                    
                    <div class="grid md:grid-cols-2 gap-6 mb-8">
                        <div class="feature-card bg-white border border-gray-200 rounded-lg p-6 shadow-sm">
                            <div class="flex items-center mb-4">
                                <div class="w-10 h-10 rounded-full bg-blue-100 flex items-center justify-center mr-3">
                                    <i class="fas fa-map text-blue-600"></i>
                                </div>
                                <h3 class="text-xl font-semibold text-gray-800">TreeMap</h3>
                            </div>
                            <p class="text-gray-600">存储键值对（key-value pairs），根据键的自然顺序或自定义比较器排序</p>
                        </div>
                        <div class="feature-card bg-white border border-gray-200 rounded-lg p-6 shadow-sm">
                            <div class="flex items-center mb-4">
                                <div class="w-10 h-10 rounded-full bg-green-100 flex items-center justify-center mr-3">
                                    <i class="fas fa-layer-group text-green-600"></i>
                                </div>
                                <h3 class="text-xl font-semibold text-gray-800">TreeSet</h3>
                            </div>
                            <p class="text-gray-600">只存储单一元素，根据元素的自然顺序或自定义比较器排序</p>
                        </div>
                    </div>
                </div>
            </div>
        </section>

        <!-- Comparison Section -->
        <section class="mb-16">
            <div class="bg-white rounded-xl shadow-md overflow-hidden">
                <div class="p-8">
                    <div class="flex items-center mb-6">
                        <div class="w-12 h-12 rounded-full bg-indigo-100 flex items-center justify-center mr-4">
                            <i class="fas fa-balance-scale text-indigo-600 text-xl"></i>
                        </div>
                        <h2 class="text-2xl font-bold text-gray-800">比较机制详解</h2>
                    </div>
                    
                    <div class="mb-8">
                        <h3 class="text-xl font-semibold text-gray-800 mb-4">1. 自然顺序（Natural Ordering）</h3>
                        <p class="text-gray-700 mb-4"><span class="font-semibold">Comparable</span> 是一个函数式接口，它要求实现类定义一个 <code class="bg-gray-100 px-2 py-1 rounded">compareTo</code> 方法。<code class="bg-gray-100 px-2 py-1 rounded">compareTo</code> 方法用于比较两个相同类型的对象，返回一个整数结果：</p>
                        <ul class="list-disc pl-6 mb-6 text-gray-700 space-y-2">
                            <li><span class="font-semibold">负数</span> 表示当前对象小于比较对象</li>
                            <li><span class="font-semibold">0</span> 表示两个对象相等</li>
                            <li><span class="font-semibold">正数</span> 表示当前对象大于比较对象</li>
                        </ul>
                        
                        <div class="code-block mb-8">
                            <div class="code-header">
                                <span>Comparable 示例</span>
                                <span><i class="fas fa-code"></i></span>
                            </div>
                            <pre><code>public class Student implements Comparable&lt;Student&gt; {
    private String name;
    private int score;

    @Override
    public int compareTo(Student other) {
        return Integer.compare(this.score, other.score);
    }
    
    // 其他方法...
}</code></pre>
                        </div>
                    </div>
                    
                    <div>
                        <h3 class="text-xl font-semibold text-gray-800 mb-4">2. 自定义比较器（Custom Comparator）</h3>
                        <p class="text-gray-700 mb-4"><span class="font-semibold">Comparator</span> 也是一个函数式接口，它提供了一个 <code class="bg-gray-100 px-2 py-1 rounded">compare</code> 方法，可以用来比较两个对象。与 <code class="bg-gray-100 px-2 py-1 rounded">Comparable</code> 不同，<code class="bg-gray-100 px-2 py-1 rounded">Comparator</code> 更灵活，因为它可以定义多个不同的比较器来对同一类型的对象进行不同方式的排序。</p>
                        
                        <div class="code-block mb-6">
                            <div class="code-header">
                                <span>Comparator 示例</span>
                                <span><i class="fas fa-code"></i></span>
                            </div>
                            <pre><code>public class StudentComparatorByName implements Comparator&lt;Student&gt; {
    @Override
    public int compare(Student s1, Student s2) {
        return s1.getName().compareTo(s2.getName());
    }
}

// 使用方式
TreeSet&lt;Student&gt; nameSortedSet = new TreeSet<>(new StudentComparatorByName());</code></pre>
                        </div>
                    </div>
                </div>
            </div>
        </section>

        <!-- Usage Section -->
        <section class="mb-16">
            <div class="bg-white rounded-xl shadow-md overflow-hidden">
                <div class="p-8">
                    <div class="flex items-center mb-6">
                        <div class="w-12 h-12 rounded-full bg-blue-100 flex items-center justify-center mr-4">
                            <i class="fas fa-laptop-code text-blue-600 text-xl"></i>
                        </div>
                        <h2 class="text-2xl font-bold text-gray-800">实际应用示例</h2>
                    </div>
                    
                    <div class="grid md:grid-cols-2 gap-8">
                        <div>
                            <h3 class="text-xl font-semibold text-gray-800 mb-4">TreeMap 示例</h3>
                            <div class="code-block mb-6">
                                <div class="code-header">
                                    <span>TreeMapDemo.java</span>
                                    <span><i class="fas fa-code"></i></span>
                                </div>
                                <pre><code>TreeMap&lt;Student, String&gt; studentMap = new TreeMap<>();
studentMap.put(new Student("Alice", 85), "A班");
studentMap.put(new Student("Bob", 92), "B班");
studentMap.put(new Student("Charlie", 78), "C班");

// 按成绩排序输出
for (Student student : studentMap.keySet()) {
    System.out.println(student);
}</code></pre>
                            </div>
                            <p class="text-gray-700">默认情况下，<code class="bg-gray-100 px-2 py-1 rounded">TreeMap</code> 使用键的自然顺序（<code class="bg-gray-100 px-2 py-1 rounded">compareTo</code>）进行排序。</p>
                        </div>
                        
                        <div>
                            <h3 class="text-xl font-semibold text-gray-800 mb-4">TreeSet 示例</h3>
                            <div class="code-block mb-6">
                                <div class="code-header">
                                    <span>TreeSetDemo.java</span>
                                    <span><i class="fas fa-code"></i></span>
                                </div>
                                <pre><code>TreeSet&lt;Student&gt; studentSet = new TreeSet<>();
studentSet.add(new Student("Alice", 85));
studentSet.add(new Student("Bob", 92));
studentSet.add(new Student("Charlie", 78));

// 按成绩排序输出
for (Student student : studentSet) {
    System.out.println(student);
}</code></pre>
                            </div>
                            <p class="text-gray-700">同样地，<code class="bg-gray-100 px-2 py-1 rounded">TreeSet</code> 默认使用元素的自然顺序进行排序。</p>
                        </div>
                    </div>
                    
                    <div class="mt-8">
                        <h3 class="text-xl font-semibold text-gray-800 mb-4">自定义排序示例</h3>
                        <div class="code-block">
                            <div class="code-header">
                                <span>CustomSortDemo.java</span>
                                <span><i class="fas fa-code"></i></span>
                            </div>
                            <pre><code>// 按姓名排序的TreeMap
Comparator&lt;Student&gt; nameComparator = 
    Comparator.comparing(Student::getName);
TreeMap&lt;Student, String&gt; nameSortedMap = 
    new TreeMap<>(nameComparator);

// 按成绩降序排序的TreeSet
Comparator&lt;Student&gt; scoreDescComparator = 
    (s1, s2) -> Integer.compare(s2.getScore(), s1.getScore());
TreeSet&lt;Student&gt; scoreDescSet = 
    new TreeSet<>(scoreDescComparator);</code></pre>
                        </div>
                    </div>
                </div>
            </div>
        </section>

        <!-- Comparison Table -->
        <section class="mb-16">
            <div class="bg-white rounded-xl shadow-md overflow-hidden">
                <div class="p-8">
                    <div class="flex items-center mb-6">
                        <div class="w-12 h-12 rounded-full bg-yellow-100 flex items-center justify-center mr-4">
                            <i class="fas fa-table text-yellow-600 text-xl"></i>
                        </div>
                        <h2 class="text-2xl font-bold text-gray-800">TreeMap 与 TreeSet 对比</h2>
                    </div>
                    
                    <div class="overflow-x-auto">
                        <table class="comparison-table rounded-lg overflow-hidden">
                            <thead>
                                <tr>
                                    <th class="rounded-tl-lg">特性</th>
                                    <th>TreeMap</th>
                                    <th class="rounded-tr-lg">TreeSet</th>
                                </tr>
                            </thead>
                            <tbody>
                                <tr>
                                    <td>数据结构</td>
                                    <td>基于红黑树的键值对映射</td>
                                    <td>基于红黑树的集合</td>
                                </tr>
                                <tr>
                                    <td>存储内容</td>
                                    <td>键值对（key-value pairs）</td>
                                    <td>单一元素</td>
                                </tr>
                                <tr>
                                    <td>排序依据</td>
                                    <td>键（Key）的自然顺序或自定义比较器</td>
                                    <td>元素的自然顺序或自定义比较器</td>
                                </tr>
                                <tr>
                                    <td>实现接口</td>
                                    <td>实现 Map 接口</td>
                                    <td>实现 Set 接口</td>
                                </tr>
                                <tr>
                                    <td>时间复杂度</td>
                                    <td>插入、删除、查找均为 O(log n)</td>
                                    <td>插入、删除、查找均为 O(log n)</td>
                                </tr>
                                <tr>
                                    <td class="rounded-bl-lg">典型用途</td>
                                    <td>需要按键排序的键值对存储</td>
                                    <td class="rounded-br-lg">需要有序且不重复的元素集合</td>
                                </tr>
                            </tbody>
                        </table>
                    </div>
                </div>
            </div>
        </section>

        <!-- Visualization Section -->
        <section id="visualization" class="mb-16">
            <div class="bg-white rounded-xl shadow-md overflow-hidden">
                <div class="p-8">
                    <div class="flex items-center mb-6">
                        <div class="w-12 h-12 rounded-full bg-purple-100 flex items-center justify-center mr-4">
                            <i class="fas fa-project-diagram text-purple-600 text-xl"></i>
                        </div>
                        <h2 class="text-2xl font-bold text-gray-800">可视化关系图</h2>
                    </div>
                    
                    <div class="mermaid bg-gray-50 p-6 rounded-lg">
                        %%{init: {'theme': 'base', 'themeVariables': { 'primaryColor': '#f5f5f5', 'primaryTextColor': '#333', 'primaryBorderColor': '#ddd', 'lineColor': '#a777e3'}}}%%
                        graph TD
                            A[TreeMap & TreeSet] --> B[基于红黑树实现]
                            B --> C[保持元素有序]
                            C --> D[时间复杂度 O(log n)]
                            
                            A --> E[排序机制]
                            E --> F[自然顺序 Comparable]
                            E --> G[自定义顺序 Comparator]
                            
                            F --> H[实现 compareTo 方法]
                            G --> I[实现 compare 方法]
                            
                            A --> J[TreeMap 特性]
                            J --> K[存储键值对]
                            J --> L[按键排序]
                            
                            A --> M[TreeSet 特性]
                            M --> N[存储单一元素]
                            M --> O[按元素排序]
                    </div>
                    
                    <div class="mt-8 grid md:grid-cols-2 gap-8">
                        <div>
                            <h3 class="text-xl font-semibold text-gray-800 mb-4">TreeMap 内部结构</h3>
                            <div class="mermaid bg-gray-50 p-6 rounded-lg">
                                %%{init: {'theme': 'base', 'themeVariables': { 'primaryColor': '#f5f5f5', 'primaryTextColor': '#333', 'primaryBorderColor': '#ddd', 'lineColor': '#6e8efb'}}}%%
                                graph TD
                                    Root[Root: (Bob,92)] --> Left[(Alice,85)]
                                    Root --> Right[(Charlie,78)]
                                    style Root fill:#6e8efb,color:white
                                    style Left fill:#a777e3,color:white
                                    style Right fill:#a777e3,color:white
                            </div>
                        </div>
                        <div>
                            <h3 class="text-xl font-semibold text-gray-800 mb-4">TreeSet 内部结构</h3>
                            <div class="mermaid bg-gray-50 p-6 rounded-lg">
                                %%{init: {'theme': 'base', 'themeVariables': { 'primaryColor': '#f5f5f5', 'primaryTextColor': '#333', 'primaryBorderColor': '#ddd', 'lineColor': '#48bb78'}}}%%
                                graph TD
                                    Root[Root: 92] --> Left[85]
                                    Root --> Right[78]
                                    style Root fill:#48bb78,color:white
                                    style Left fill:#38a169,color:white
                                    style Right fill:#38a169,color:white
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </section>

        <!-- Best Practices Section -->
        <section class="mb-16">
            <div class="bg-white rounded-xl shadow-md overflow-hidden">
                <div class="p-8">
                    <div class="flex items-center mb-6">
                        <div class="w-12 h-12 rounded-full bg-green-100 flex items-center justify-center mr-4">
                            <i class="fas fa-check-circle text-green-600 text-xl"></i>
                        </div>
                        <h2 class="text-2xl font-bold text-gray-800">最佳实践与注意事项</h2>
                    </div>
                    
                    <div class="space-y-6">
                        <div class="flex items-start">
                            <div class="flex-shrink-0 mt-1 mr-4 text-green-500">
                                <i class="fas fa-check-circle"></i>
                            </div>
                            <div>
                                <h3 class="text-lg font-semibold text-gray-800 mb-2">一致性要求</h3>
                                <p class="text-gray-700">确保 <code class="bg-gray-100 px-2 py-1 rounded">compareTo</code> 或 <code class="bg-gray-100 px-2 py-1 rounded">compare</code> 方法与 <code class="bg-gray-100 px-2 py-1 rounded">equals</code> 方法保持一致。如果 <code class="bg-gray-100 px-2 py-1 rounded">compareTo</code> 返回 0，那么 <code class="bg-gray-100 px-2 py-1 rounded">equals</code> 应该返回 true。</p>
                            </div>
                        </div>
                        
                        <div class="flex items-start">
                            <div class="flex-shrink-0 mt-1 mr-4 text-yellow-500">
                                <i class="fas fa-exclamation-triangle"></i>
                            </div>
                            <div>
                                <h3 class="text-lg font-semibold text-gray-800 mb-2">不可变对象</h3>
                                <p class="text-gray-700">建议将作为键或元素的类设计为不可变的。如果对象在放入集合后发生变化，可能导致排序不一致。</p>
                            </div>
                        </div>
                        
                        <div class="flex items-start">
                            <div class="flex-shrink-0 mt-1 mr-4 text-blue-500">
                                <i class="fas fa-lightbulb"></i>
                            </div>
                            <div>
                                <h3 class="text-lg font-semibold text-gray-800 mb-2">性能考虑</h3>
                                <p class="text-gray-700">虽然红黑树保证了 O(log n) 的操作时间，但对于频繁插入删除的场景，可能考虑 <code class="bg-gray-100 px-2 py-1 rounded">HashMap</code> 和 <code class="bg-gray-100 px-2 py-1 rounded">HashSet</code> 以获得更好的平均性能。</p>
                            </div>
                        </div>
                        
                        <div class="flex items-start">
                            <div class="flex-shrink-0 mt-1 mr-4 text-purple-500">
                                <i class="fas fa-random"></i>
                            </div>
                            <div>
                                <h3 class="text-lg font-semibold text-gray-800 mb-2">多字段排序</h3>
                                <p class="text-gray-700">可以使用 <code class="bg-gray-100 px-2 py-1 rounded">Comparator.comparing().thenComparing()</code> 链式调用实现多字段排序。</p>
                                <div class="code-block mt-3">
                                    <pre><code>Comparator&lt;Student&gt; complexComparator = Comparator
    .comparing(Student::getScore)
    .thenComparing(Student::getName);</code></pre>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </section>
    </main>

    <!-- Footer -->
    <footer class="bg-gray-900 text-gray-300 py-8">
        <div class="container mx-auto max-w-5xl px-4">
            <div class="flex flex-col md:flex-row justify-between items-center">
                <div class="mb-4 md:mb-0">
                    <p class="text-xl font-semibold text-white">技术小馆</p>
                    <p class="mt-1">探索编程世界的奥秘</p>
                </div>
                <div>
                    <a href="http://www.yuque.com/jtostring" class="text-purple-300 hover:text-white transition duration-300">
                        <i class="fas fa-external-link-alt mr-2"></i>http://www.yuque.com/jtostring
                    </a>
                </div>
            </div>
            <div class="mt-8 pt-6 border-t border-gray-800 text-center text-sm text-gray-500">
                &copy; 2023 技术小馆. 保留所有权利.
            </div>
        </div>
    </footer>

    <script>
        mermaid.initialize({
            startOnLoad: true,
            theme: 'default',
            flowchart: {
                useMaxWidth: false,
                htmlLabels: true,
                curve: 'basis'
            }
        });
        
        // 平滑滚动
        document.querySelectorAll('a[href^="#"]').forEach(anchor => {
            anchor.addEventListener('click', function (e) {
                e.preventDefault();
                document.querySelector(this.getAttribute('href')).scrollIntoView({
                    behavior: 'smooth'
                });
            });
        });
    </script>
</body>
</html>
```